package jdk8的特性.函数式接口.过滤式案例;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：idea
 * 日期：2018/8/19
 * 描述：
 */
public class FruitFilter {

    public static <T> List<T> filter(List<T> list,Predicate<T> p){
        List<T> resultList=new ArrayList<>();
        for (T t : list) {
            if(p.test(t)){
                resultList.add(t);
            }
        }
        return resultList;
    }
    public static void main(String[] args) {
        Apple apple1=new Apple("red",11);
        Apple apple2=new Apple("blue",15);
        Apple apple3=new Apple("red",15);
        List<Apple> appleList=new ArrayList<>();
        appleList.add(apple1);
        appleList.add(apple2);
        appleList.add(apple3);

        List<Apple> resultList=filter(appleList,apple -> "red".equals(apple.getColor()));
        for (Apple apple : resultList) {
            System.out.println(apple.toString());
        }
    }

}
